package icbc.nav.control
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import icbc.app.AppGlobal;
	import icbc.app.AppText;
	import icbc.app.control.Alert;
	import icbc.nav.entity.MenuChild;
	
	import org.osflash.signals.Signal;
	
	public class BotMenuControl extends Sprite
	{
		protected var panel:Sprite;
		public var viewWidth:uint;
		public var viewHeight:uint;
		private var menuData:Array = [];
		public var onClick:Signal = new Signal(String);
		private var menuHome:BotMenuItemControl;
		private var menuAccessBanking:BotMenuItemControl;
		
		public function BotMenuControl(menuData:Array)
		{
			super();
			this.menuData = menuData;
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			stage.addEventListener(Event.RESIZE, initMain);
			stage.dispatchEvent(new Event(Event.RESIZE));
		}
		
		private function initMain(evt:Event = null):void
		{
			if(stage.stageWidth>0)
				stage.removeEventListener(Event.RESIZE, initMain);
			else return;
			initContent();
		}
		
		private function initContent():void
		{
			panel = setBasePanel();
			addChild(panel);
			
			var itemWidth:uint = 41;
			var itemHeight:uint = 41;
			
			var gapx:uint = 0;
			var posx:uint = gapx;
			var panelHeightHalf:uint = panel.height/2;
			
			for(var i:uint = 0; i<menuData.length; i++)
			{
				var menuItem:MenuChild = menuData[i];
				var item:BotMenuItemControl = new BotMenuItemControl({icon:menuItem.icon, name:menuItem.name}, itemWidth, itemHeight);
				item.addEventListener(BotMenuItemControl.EVENT_CLICK, itemClickHandler);
				panel.addChild(item);
				item.x = posx; posx += item.width - 1;
				item.y = panelHeightHalf - itemHeight/2;
				if(menuItem.name == AppText.MENU_HOME)
				{
					menuHome = item;
				}
				if(menuItem.name == AppText.MENU_ACCESSBANKING)
				{
					menuAccessBanking = item;
				}
			}
		}
		
		private function itemClickHandler(evt:Event):void
		{
			var itemClicked:BotMenuItemControl = evt.target as BotMenuItemControl;
			for(var i:uint = 0; i<panel.numChildren; i++)
			{
				if(panel.getChildAt(i) is BotMenuItemControl)
				{
					var item:BotMenuItemControl = panel.getChildAt(i) as BotMenuItemControl;
					if(itemClicked.data.icon != item.data.icon)
					{
						item.clearState();
					}
				}
			}
			onClick.dispatch(itemClicked.data.name);
		}
		
		private function setBasePanel():Sprite
		{
			viewWidth = 100;
			viewHeight = 41;
			return buildPlainPanel();
		}
		
		private function buildPlainPanel():Sprite
		{
			var base:Sprite = new Sprite();
			base.graphics.beginFill(0xE6E8D8,0);
			base.graphics.drawRect(0, 0, viewWidth, viewHeight);
			base.graphics.endFill();
			return base;
		}
		
		public function clearMenuState():void
		{
			if(menuHome != null)
			{
				menuHome.clearState();
			}
			if(menuAccessBanking != null)
			{
				menuAccessBanking.clearState();
			}
		}

		public function setMenuState(menuNro:int):void
		{
			if(menuNro == 0)
			{
				menuHome.setClickedState();
			} else {
				menuAccessBanking.setClickedState();
			}
		}
	}
}